package com.cloudsigns.factorymanager2.engineering.adapter;

import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.DailyFeedBackStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.DailyOrderStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/12.
 */

public class TableContentEngineeringInstallForManagerAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> dataList = new ArrayList<>();

    private List<String> tableHeadDataList = new ArrayList<>();
    private String tag = "";
    private long minDate;
    private LayoutInflater layoutInflater;
    private ItemClickListener itemClickListener;

    public TableContentEngineeringInstallForManagerAdapter(Activity mcontext, String tag) {
        this.context = context;
        this.tag = tag;
        layoutInflater = mcontext.getLayoutInflater();
    }

    public void setData(List<T> feedbackStatuList) {
        this.dataList.clear();
        if (null != feedbackStatuList) {
            this.dataList.addAll(feedbackStatuList);
        }
    }

    public void setHeadData(List<String> headDataList) {
        this.tableHeadDataList.clear();
        if (null != headDataList) {
            this.tableHeadDataList.addAll(headDataList);
        }
    }

    public void setOnclickListener(ItemClickListener clickListener) {
        this.itemClickListener = clickListener;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_table_content_list, null);
            holder.ll_content = convertView.findViewById(R.id.ll_content);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (null != tag) {
            if (tag.equals("PatrolStatusTableForManagerActivity")) {

                holder.ll_content.removeAllViews();

                MediaBean mediaBean = (MediaBean) dataList.get(position);
                String feedbackDate = DateUtil.getYyyy_Mm_Dd(mediaBean.getHandleDate());
                String currentDate = DateUtil.getYyyy_Mm_Dd(System.currentTimeMillis());
                CommonEnum.FeedBackState feedBackState = mediaBean.getState();

                CommonEnum.FeedBackType type = mediaBean.getType();

                if (null != tableHeadDataList) {
                    for (int i = 0; i < tableHeadDataList.size(); i++) {

                        final String date = tableHeadDataList.get(i);

                        View view = layoutInflater.inflate(R.layout.item_table_content_install_statu, null);

                        TextView img_feedback_state = view.findViewById(R.id.img_feedback_state);
                        View line_order_statu = view.findViewById(R.id.line_order_statu);

                        line_order_statu.setVisibility(View.VISIBLE);
                        line_order_statu.setBackgroundResource(R.color.rgb_E6E6E6);

                        View groupView = view.findViewById(R.id.rl_item_status);
                        groupView.setTag(mediaBean);

                        if (feedBackState == CommonEnum.FeedBackState.COMPLETE) {
                            if (date.equals(feedbackDate)) {
                                img_feedback_state.setVisibility(View.VISIBLE);
                                img_feedback_state.setBackgroundResource(R.color.rgb_2C9A2D);
                            }

                        } else if (feedBackState == CommonEnum.FeedBackState.WORKING) {
                            if (date.equals(currentDate)) {
                                img_feedback_state.setVisibility(View.VISIBLE);
                                img_feedback_state.setBackgroundResource(R.color.rgb_A6A6A6);
                            }


                        } else if (feedBackState == CommonEnum.FeedBackState.WAIT_CONFIRM) {
                            if (date.equals(currentDate)) {
                                img_feedback_state.setVisibility(View.VISIBLE);
                                img_feedback_state.setBackgroundResource(R.color.rgb_FFC000);
                            }

                        } else if (feedBackState == CommonEnum.FeedBackState.EXCEPTION) {
                            if (date.equals(currentDate)) {
                                img_feedback_state.setVisibility(View.VISIBLE);
                                img_feedback_state.setBackgroundResource(R.color.rgb_C00000);
                            }
                        } else if (feedBackState == CommonEnum.FeedBackState.PAUSE) {
                            if (date.equals(currentDate)) {
                                img_feedback_state.setVisibility(View.VISIBLE);
                                img_feedback_state.setBackgroundResource(R.color.rgb_BF9000);
                            }
                        } else {
                            img_feedback_state.setVisibility(View.GONE);
                        }


                        if (img_feedback_state.getVisibility() == View.VISIBLE) {

                            groupView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (null != itemClickListener) {
                                        itemClickListener.itemClick(view, position);
                                    }
                                }
                            });
                        }

                        holder.ll_content.addView(view);

                    }


                }


//                MediaBean mediaBean = (MediaBean) mListData.get(position);
//                CommonEnum.FeedBackState feedBackState = mediaBean.getState();
//                long handleDate = mediaBean.getHandleDate();
//                String handleDateStr = DateUtil.getYyyy_Mm_Dd(handleDate);
//
//                for (String date : tableHeadDataList) {
//
//                    View view = layoutInflater.inflate(R.layout.item_table_content_install_statu, null); //item_table_content_patrol_status
//                    ImageView img_state = view.findViewById(R.id.img_state);
//                    if (date.equals(handleDateStr) && null != feedBackState) {
//                        if (feedBackState == CommonEnum.FeedBackState.WORKING) {
//                            img_state.setBackgroundResource(R.mipmap.dot_green);
//                        } else if (feedBackState == CommonEnum.FeedBackState.WAIT_CONFIRM) {
//                            img_state.setBackgroundResource(R.mipmap.dot_grey);
//
//                        } else if (feedBackState == CommonEnum.FeedBackState.COMPLETE) {
//                            img_state.setBackgroundResource(R.mipmap.dot_green);
//                        } else if (feedBackState == CommonEnum.FeedBackState.EXCEPTION) {
//                            img_state.setBackgroundResource(R.mipmap.dot_red);
//                        } else if (feedBackState == CommonEnum.FeedBackState.PAUSE) {
//                            img_state.setBackgroundResource(R.mipmap.dot_yellow);
//                        }
//                    }
//                    holder.ll_content.addView(view);
//                }
            } else if (tag.equals("EngineeringInstallStatusTableForManagerActivity1")) {
//                View view = layoutInflater.inflate(R.layout.item_table_content_patrol_status, null);
//                LinearLayout ll_table_content = view.findViewById(R.id.ll_table_content);

                holder.ll_content.removeAllViews();

                EngineeringInstallStatuBean engineeringInstallStatuBean = (EngineeringInstallStatuBean) dataList.get(position);
                List<DailyFeedBackStatuBean> dailyFeedBackStatuBeanList = engineeringInstallStatuBean.getDailyFeedbackByDateList();
                List<DailyOrderStatuBean> dailyOrderStatuBeanList = engineeringInstallStatuBean.getDailyOrderStatusList();

                if (null != tableHeadDataList) {
                    for (int i = 0; i < tableHeadDataList.size(); i++) {

                        View view = layoutInflater.inflate(R.layout.item_table_content_install_statu, null);

                        TextView img_feedback_state = view.findViewById(R.id.img_feedback_state);
                        View line_order_statu = view.findViewById(R.id.line_order_statu);
                        View groupView = view.findViewById(R.id.rl_item_status);


                        img_feedback_state.setVisibility(View.GONE);


                        final String date = tableHeadDataList.get(i);

                        if (null != dailyFeedBackStatuBeanList) {
                            for (DailyFeedBackStatuBean dailyFeedBackStatuBean : dailyFeedBackStatuBeanList) {
                                String feedbackDate = dailyFeedBackStatuBean.getDateStr();
                                CommonEnum.FeedBackState state = dailyFeedBackStatuBean.getState();
                                if (date.equals(feedbackDate)) {
                                    groupView.setTag(dailyFeedBackStatuBean);
                                    if (null != state) {
                                        if (state == CommonEnum.FeedBackState.EXCEPTION) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_C00000);
                                        } else if (state == CommonEnum.FeedBackState.WAIT_CONFIRM) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_FFC000);
                                        } else if (state == CommonEnum.FeedBackState.WORKING) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_A6A6A6);
                                        } else if (state == CommonEnum.FeedBackState.COMPLETE) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_2C9A2D);
                                        } else if (state == CommonEnum.FeedBackState.OUT_OF_PLAN) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_2F5597);
                                        } else if (state == CommonEnum.FeedBackState.NO_FEEDBACK) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_ED7D31);

                                        } else if (state == CommonEnum.FeedBackState.PAUSE) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_BF9000);
                                        }else if (state == CommonEnum.FeedBackState.CREATE) {
                                            img_feedback_state.setVisibility(View.VISIBLE);
                                            img_feedback_state.setBackgroundResource(R.color.rgb_ED7D31);
                                        } else {
                                            img_feedback_state.setVisibility(View.GONE);
                                        }
                                    }

                                }

                            }
                        }


                        if (null != dailyOrderStatuBeanList) {
                            for (DailyOrderStatuBean dailyOrderStatuBean : dailyOrderStatuBeanList) {
                                String feedbackDate = dailyOrderStatuBean.getDateStr();
                                CommonEnum.OrderCycleStatus state = dailyOrderStatuBean.getState();

                                if (date.equals(feedbackDate)) {
                                    if (state == CommonEnum.OrderCycleStatus.PLAN_CYCLE) {
                                        line_order_statu.setVisibility(View.VISIBLE);
                                        line_order_statu.setBackgroundResource(R.color.rgb_BFBFBF);
                                    } else if (state == CommonEnum.OrderCycleStatus.CYCLE_ADJUSTMENT) {
                                        line_order_statu.setVisibility(View.VISIBLE);
                                        line_order_statu.setBackgroundResource(R.color.rgb_9DC3E6);
                                    } else if (state == CommonEnum.OrderCycleStatus.READJUSTMENT) {
                                        line_order_statu.setVisibility(View.VISIBLE);
                                        line_order_statu.setBackgroundResource(R.color.rgb_2F5597);
                                    } else if (state == CommonEnum.OrderCycleStatus.PAUSE) {
                                        line_order_statu.setVisibility(View.VISIBLE);
                                        line_order_statu.setBackgroundResource(R.color.rgb_BF9000);
                                    } else if (state == CommonEnum.OrderCycleStatus.COMPLETE) {
                                        line_order_statu.setVisibility(View.VISIBLE);
                                        line_order_statu.setBackgroundResource(R.color.rgb_2C9A2D);
                                    }
                                }
                            }
                        }


                        if (img_feedback_state.getVisibility() == View.VISIBLE) {

                            groupView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (null != itemClickListener) {
                                        itemClickListener.itemClick(view, position);
                                    }
                                }
                            });
                        }

                        holder.ll_content.addView(view);

                    }
                }

//                if (null != dailyFeedBackStatuBeanList) {
//                    for (int i = 0; i < dailyFeedBackStatuBeanList.size(); i++) {
//
//                        final DailyFeedBackStatuBean dailyFeedBackStatuBean = dailyFeedBackStatuBeanList.get(i);
//
//                        View view = layoutInflater.inflate(R.layout.item_table_content_install_statu, null);
//
//                        TextView img_feedback_state = view.findViewById(R.id.img_feedback_state);
//                        View line_order_statu = view.findViewById(R.id.line_order_statu);
//
//                        View groupView = view.findViewById(R.id.rl_item_status);
//                        groupView.setTag(dailyFeedBackStatuBean);
//
//                        img_feedback_state.setVisibility(View.GONE);
//
//                        CommonEnum.FeedBackState feedBackState = dailyFeedBackStatuBean.getState();
//                        if (feedBackState == CommonEnum.FeedBackState.WORKING) {
//                            img_feedback_state.setBackgroundResource(R.color.rgb_A6A6A6);
//
//                            img_feedback_state.setVisibility(View.VISIBLE);
//
//                        } else if (feedBackState == CommonEnum.FeedBackState.WAIT_CONFIRM) {
//                            img_feedback_state.setBackgroundResource(R.color.rgb_FFC000);
//
////                            img_feedback_state.setBackgroundResource(R.mipmap.dot_yellow);
//                            img_feedback_state.setVisibility(View.VISIBLE);
//
//                        } else if (feedBackState == CommonEnum.FeedBackState.COMPLETE) {
//                            img_feedback_state.setBackgroundResource(R.color.rgb_2C9A2D);
//
////                            img_feedback_state.setBackgroundResource(R.mipmap.dot_green);
//                            img_feedback_state.setVisibility(View.VISIBLE);
//                        } else if (feedBackState == CommonEnum.FeedBackState.EXCEPTION) {
//                            img_feedback_state.setBackgroundResource(R.color.rgb_C00000);
//
////                            img_feedback_state.setBackgroundResource(R.mipmap.dot_red);
//                            img_feedback_state.setVisibility(View.VISIBLE);
//                        } else if (feedBackState == CommonEnum.FeedBackState.PAUSE) {
//                            img_feedback_state.setBackgroundResource(R.color.rgb_BF9000);
//
////                            img_feedback_state.setBackgroundResource(R.mipmap.dot_yellow);
//                            img_feedback_state.setVisibility(View.VISIBLE);
//                        } else if (feedBackState == CommonEnum.FeedBackState.OUT_OF_PLAN) {
////                            img_feedback_state.setBackgroundColor(context.getResources().getColor(R.color.rgb_F5597));
//                            img_feedback_state.setBackgroundResource(R.color.rgb_F5597);
////                            img_feedback_state.setBackgroundResource(R.mipmap.bg_circle_blue);
//                            img_feedback_state.setVisibility(View.VISIBLE);
//                        } else if (feedBackState == CommonEnum.FeedBackState.NO_FEEDBACK) {
//
//                            img_feedback_state.setBackgroundResource(R.color.rgb_ED7D31);
////                            img_feedback_state.setBackgroundResource(R.mipmap.bg_circle_f5b73d);
//                            img_feedback_state.setVisibility(View.VISIBLE);
//                        } else {
//                            img_feedback_state.setVisibility(View.GONE);
//                        }
//
//
//                        line_order_statu.setVisibility(View.GONE);
//
//                        if (i < dailyOrderStatuBeanList.size()) {
//                            DailyOrderStatuBean dailyOrderStatuBean = dailyOrderStatuBeanList.get(i);
//                            CommonEnum.OrderCycleStatus state = dailyOrderStatuBean.getState();
//                            if (null != state) {
//                                if (state == CommonEnum.OrderCycleStatus.PLAN_CYCLE) {
////                                    line_order_statu.setBackgroundResource(R.mipmap.btn_long_bg_gray);
//                                    img_feedback_state.setBackgroundResource(R.color.rgb_BFBFBF);
//
//                                    line_order_statu.setVisibility(View.VISIBLE);
//                                } else if (state == CommonEnum.OrderCycleStatus.CYCLE_ADJUSTMENT) {
////                                    line_order_statu.setBackgroundResource(R.mipmap.btn_long_bg_blue);
//                                    img_feedback_state.setBackgroundResource(R.color.rgb_9DC3E6);
//
//                                    line_order_statu.setVisibility(View.VISIBLE);
//                                } else if (state == CommonEnum.OrderCycleStatus.READJUSTMENT) {
////                                    line_order_statu.setBackgroundResource(R.mipmap.btn_long_bg_blue);
//                                    img_feedback_state.setBackgroundResource(R.color.rgb_2F5597);
//
//                                    line_order_statu.setVisibility(View.VISIBLE);
//                                } else if (state == CommonEnum.OrderCycleStatus.PAUSE) {
////                                    line_order_statu.setBackgroundResource(R.mipmap.btn_long_bg_red);
//                                    img_feedback_state.setBackgroundResource(R.color.rgb_BF9000);
//
//                                    line_order_statu.setVisibility(View.VISIBLE);
//                                } else if (state == CommonEnum.OrderCycleStatus.COMPLETE) {
////                                    line_order_statu.setBackgroundResource(R.mipmap.btn_long_bg_green);
//                                    img_feedback_state.setBackgroundResource(R.color.rgb_2C9A2D);
//
//                                    line_order_statu.setVisibility(View.VISIBLE);
//                                } else {
//                                    line_order_statu.setVisibility(View.GONE);
//                                }
//                            }
//                        }
//
//                        groupView.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                if (null != itemClickListener) {
//                                    itemClickListener.itemClick(view, position);
//                                }
//                            }
//                        });
//
//                        holder.ll_content.addView(view);
//
//                    }
//
//
//                }

            } else if (tag.equals("PublicationStatusTableForManagerActivity2")) {
                holder.ll_content.removeAllViews();

                MediaBean mediaBean = (MediaBean) dataList.get(position);
                if (mediaBean.isPContent()) {
                    if (null != tableHeadDataList) {
                        for (int i = 0; i < tableHeadDataList.size(); i++) {
                            View view = layoutInflater.inflate(R.layout.item_table_content_install_statu, null);

                            View rl_blank = view.findViewById(R.id.rl_blank);
                            View groupView = view.findViewById(R.id.rl_item_status);

                            groupView.setVisibility(View.GONE);
                            rl_blank.setVisibility(View.VISIBLE);


                            holder.ll_content.addView(view);

                        }
                    }
                } else {

                    String feedbackDate = DateUtil.getYyyy_Mm_Dd(mediaBean.getFeedbackDate());
                    CommonEnum.MediaStatus mediaStatus = mediaBean.getStatus();
                    String currentDate = DateUtil.getYyyy_Mm_Dd(System.currentTimeMillis());

                    CommonEnum.FeedBackType type = mediaBean.getType();


                    if (null != tableHeadDataList) {
                        for (int i = 0; i < tableHeadDataList.size(); i++) {

                            final String date = tableHeadDataList.get(i);

                            View view = layoutInflater.inflate(R.layout.item_table_content_install_statu, null);

                            TextView img_feedback_state = view.findViewById(R.id.img_feedback_state);
                            View line_order_statu = view.findViewById(R.id.line_order_statu);

                            line_order_statu.setVisibility(View.VISIBLE);
                            line_order_statu.setBackgroundResource(R.color.rgb_E6E6E6);

                            View groupView = view.findViewById(R.id.rl_item_status);
                            groupView.setTag(mediaBean);

                            if (null != mediaStatus) {
                                if (mediaStatus == CommonEnum.MediaStatus.COMPLETE) {
                                    if (feedbackDate.equals(date)) {
//                                      img_feedback_state.setBackgroundResource(R.mipmap.dot_green);
                                        img_feedback_state.setBackgroundResource(R.color.rgb_2C9A2D);
                                        img_feedback_state.setVisibility(View.VISIBLE);
                                    }
                                } else if (mediaStatus == CommonEnum.MediaStatus.PROCESSING) {
                                    if (currentDate.equals(date)) {
                                        img_feedback_state.setVisibility(View.VISIBLE);
                                        img_feedback_state.setBackgroundResource(R.color.rgb_A6A6A6);
                                    }
                                } else if (mediaStatus == CommonEnum.MediaStatus.PAUSE) {
                                    if (currentDate.equals(date)) {
                                        img_feedback_state.setVisibility(View.VISIBLE);
                                        img_feedback_state.setBackgroundResource(R.color.rgb_BF9000);
                                    }
                                } else if (mediaStatus == CommonEnum.MediaStatus.PROBLEM_APPEAR) {
                                    if (currentDate.equals(date)) {
                                        img_feedback_state.setVisibility(View.VISIBLE);
                                        img_feedback_state.setBackgroundResource(R.color.rgb_C00000);
                                    }
                                } else if (mediaStatus == CommonEnum.MediaStatus.PROBLEM_FEEDBACK) {
                                    if (currentDate.equals(date)) {
                                        img_feedback_state.setVisibility(View.VISIBLE);
                                        img_feedback_state.setBackgroundResource(R.color.rgb_FFC000);
                                    }

                                } else if (mediaStatus == CommonEnum.MediaStatus.COMPLETE_FEEDBACK) {
                                    if (currentDate.equals(date)) {
                                        img_feedback_state.setVisibility(View.VISIBLE);
                                        img_feedback_state.setBackgroundResource(R.color.rgb_FFC000);
                                    }

                                } else {
                                    img_feedback_state.setVisibility(View.GONE);
                                }

                            }


                            if (img_feedback_state.getVisibility() == View.VISIBLE) {

                                groupView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if (null != itemClickListener) {
                                            itemClickListener.itemClick(view, position);
                                        }
                                    }
                                });
                            }

                            holder.ll_content.addView(view);

                        }


                    }
                }


            }
        }


//
//       /*异常反馈*/
//        if (tag.equals("ProductStatusTableActivity")) {
//            holder.img_return.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                   /*生产日反馈*/
//                    Intent intent = new Intent(context, ProductionDailyFeedbackActivity.class);
//                    context.startActivity(intent);
//                }
//            });
//
//        } else if (tag.equals("ProductionScheduleActivity")) { //生产进度表
//            holder.img_return.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                   /*产品质检单*/
//                    Intent intent = new Intent(context, QualityInspectionOrderActivity.class);
//                    context.startActivity(intent);
//                }
//            });
//        } else if (tag.equals("EngineeringInstallationScheduleTableActivity")) { //工程安装进度表
//            holder.img_return.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                   /*竣工评价*/
//                    Intent intent = new Intent(context, CompletionOfEvaluationActivity.class);
//                    context.startActivity(intent);
//                }
//            });
//        }
//
//
//        holder.rl_item_data_1.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View view) {
////                Intent intent = new Intent(context, com.cloudsigns.factorymanager2.ProductQualitySheet.class);
////                context.startActivity(intent);
//            }
//        });

        return convertView;
    }


    class ViewHolder {
        LinearLayout ll_content;
//        RelativeLayout rl_item_data_1;
//        ImageView img_return;
    }
}
